//
//  JCMainViewController.swift
//  JCTest3
//
//  Created by Chen on 09/10/2022.
//

import Cocoa

class JCMainViewController: JCBaseViewController {
    
    private var splitView: NSSplitView = NSSplitView()
    private var topView: JCBaseView = JCBaseView()
    private var bottomView: JCBaseView = JCBaseView()
    private lazy var viewBottomView: JCNSView = JCNSView()
    private lazy var buttonBottomView: JCNSButtonView = JCNSButtonView()
    private lazy var labelBottomView: JCLabelView = JCLabelView()
    private lazy var tabBottomView: JCTabView = JCTabView()
    private lazy var tableBottomView: JCNSTableView = JCNSTableView()
    private lazy var comboBoxView: JCNSComboBoxView = JCNSComboBoxView()
    private lazy var viewButton: NSButton = NSButton()
    private lazy var btnButton: NSButton = NSButton()
    private lazy var tabButton: NSButton = NSButton()
    private lazy var labelButton: NSButton = NSButton()
    private lazy var tableButton: NSButton = NSButton()
    private lazy var comboButton: NSButton = NSButton()
    // test git
    private lazy var comboButton1: NSButton = NSButton()
    
    private lazy var scrollView: NSScrollView = {
        let scrollView = NSScrollView()
        scrollView.hasVerticalScroller = false
        scrollView.hasHorizontalScroller = false
        scrollView.autoresizingMask = [.width, .height]
        return scrollView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        self.title = "Main Window"
        self.setViews()
    }
    
}

extension JCMainViewController {
    
    /*
    // 设置默认视图大小
    func setViews() {
        // NSView
        print("viewY: \(self.view.frame.minY)")
        let viewButton = NSButton(frame: NSRect(x: 20, y: self.view.frame.maxY - 60, width: 100, height: 40))
        
//        viewButton.frame = NSRect(x: 24, y: 24, width: 100, height: 40)
        // 设置按钮的文字
        viewButton.title = "NSView"
        // 设置按钮开启状态时的文字（switch）
//        viewButton.alternateTitle = "Another"
        viewButton.bezelStyle = .rounded
        viewButton.target = self
        viewButton.action = #selector(self.viewButtonClicked)
        viewButton.setupViewBackgroundColor(color: NSColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0))
        self.view.addSubview(viewButton)
        
        // NSButton
        let buttonButton = NSButton(frame: NSRect(x: 40 + 100, y: self.view.frame.maxY - 60, width: 100, height: 40))
            
//        viewButton.frame = NSRect(x: 24, y: 24, width: 100, height: 40)
        // 设置按钮的文字
        buttonButton.title = "NSButton"
        // 设置按钮开启状态时的文字（switch）
//        viewButton.alternateTitle = "Another"
        buttonButton.bezelStyle = .rounded
        buttonButton.target = self
        buttonButton.action = #selector(self.buttonButtonClicked)
        buttonButton.setupViewBackgroundColor(color: NSColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0))
        self.view.addSubview(buttonButton)
                
    }
     */
    
    // 用NSSpliteView进行分栏，
    func setViews() {
        
//        let currentWindow = NSApplication.shared.windows.last
//        let currentWindow = NSApplication.shared.keyWindow
        self.view.setupViewBackgroundColor(color: NSColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1.0))
        let viewFrame = self.view.frame
        print("x: \(viewFrame.minX), y: \(viewFrame.minY), width: \(viewFrame.width), height: \(viewFrame.height)")
        let splitView = NSSplitView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: viewFrame.height))
        splitView.isVertical = true
        self.splitView = splitView
        self.splitView.dividerStyle = .thin
        
        // 加约束
        self.view.addConstraint(NSLayoutConstraint(item: self.splitView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: self.splitView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0))
//        self.view.addConstraint(NSLayoutConstraint(item: self.splitView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0))
//        self.view.addConstraint(NSLayoutConstraint(item: self.splitView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0))
//        self.view.addConstraint(NSLayoutConstraint(item: self.splitView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1, constant: 0))
//        self.view.addConstraint(NSLayoutConstraint(item: self.splitView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1, constant: 0))
        
        let topView = JCBaseView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: 80))
        topView.setupViewBackgroundColor(color: NSColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0))
        self.topView = topView
        // document view的尺寸大于content view的尺寸才能滚动
        self.scrollView.frame = NSRect(x: 0, y: 0, width: viewFrame.width, height: 80)
//        self.scrollView.contentSize = CGSize(width: viewFrame.width, height: 80)
        self.scrollView.documentView = self.topView

        let bottomView = JCBaseView(frame: NSRect(x: 0, y: 80, width: viewFrame.width, height: viewFrame.height - 80))
        bottomView.setupViewBackgroundColor(color: NSColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0))
        self.bottomView = bottomView

        self.splitView.addSubview(self.scrollView)
        self.splitView.addSubview(self.bottomView)
        self.view.addSubview(self.splitView)
        splitView.delegate = self
        
        self.setupTopViews()
        self.setupBottomViews()
        
    }
    
}

// MARK: - 事件
extension JCMainViewController {
    
    func setupButtonAttribute(frame: NSRect, title: String, font: NSFont, tag: Int) -> NSButton {
        let btn = NSButton(frame: frame)
        btn.title = title
        btn.font = font
//        btn.bezelStyle = .texturedSquare
        btn.setButtonType(.momentaryLight)
        btn.tag = tag
//        btn.isTransparent = true
        btn.isBordered = false
        
        btn.setupViewBackgroundColor(color: .gray)
        
        return btn
    }
    
    // 顶部视图
    func setupTopViews() {
        
        // NSView Button
        let buttonW = 150
        let buttonH = 40
        let buttonY = 20
        let buttonX = 12
        self.viewButton = self.setupButtonAttribute(frame: NSRect(x: buttonX, y: buttonY, width: buttonW, height: buttonH), title: "NSView", font: NSFont.systemFont(ofSize: 18), tag: 0)
        self.topView.addSubview(self.viewButton)
        self.viewButton.action = #selector(self.buttonButtonClicked(btn:))
        self.viewButton.setupViewBackgroundColor(color: .blue)
        
        // NSButton Button
        self.btnButton = self.setupButtonAttribute(frame: NSRect(x: buttonX * 2 + buttonW, y: buttonY, width: buttonW, height: buttonH), title: "NSButton", font: NSFont.systemFont(ofSize: 18), tag: 1)
        self.topView.addSubview(self.btnButton)
        self.btnButton.action = #selector(self.buttonButtonClicked(btn:))
        
        // Label Button
        self.labelButton = self.setupButtonAttribute(frame: NSRect(x: buttonX * 3 + buttonW * 2, y: buttonY, width: buttonW, height: buttonH), title: "NSTextField", font: NSFont.systemFont(ofSize: 18), tag: 2)
        self.topView.addSubview(self.labelButton)
        self.labelButton.action = #selector(self.buttonButtonClicked(btn:))
        
        // Tab Button
        self.tabButton = self.setupButtonAttribute(frame: NSRect(x: buttonX * 4 + buttonW * 3, y: buttonY, width: buttonW, height: buttonH), title: "NSTabView", font: NSFont.systemFont(ofSize: 18), tag: 3)
        self.topView.addSubview(self.tabButton)
        self.tabButton.action = #selector(self.buttonButtonClicked(btn:))
        
        // NSTableView Button
        self.tableButton = self.setupButtonAttribute(frame: NSRect(x: buttonX * 5 + buttonW * 4, y: buttonY, width: buttonW, height: buttonH), title: "NSTableView", font: NSFont.systemFont(ofSize: 18), tag: 4)
        self.topView.addSubview(self.tableButton)
        self.tableButton.action = #selector(self.buttonButtonClicked(btn:))
        
        // NSSpliteView Button
        self.comboButton = self.setupButtonAttribute(frame: NSRect(x: buttonX * 6 + buttonW * 5, y: buttonY, width: buttonW, height: buttonH), title: "NSComboBoxView", font: NSFont.systemFont(ofSize: 18), tag: 5)
        self.topView.addSubview(self.comboButton)
        self.comboButton.action = #selector(self.buttonButtonClicked(btn:))
        
        
        
        // 内容视图宽度必须大于scrollview的frame宽度才会滚动
        self.topView.frame = NSRect(x: 0, y: 0, width: buttonX * 6 + buttonW * 5 + buttonW + buttonX, height: 80)
        
    }
    
    // 底部视图
    func setupBottomViews() {
        
        let viewFrame = self.view.frame
        self.viewBottomView = JCNSView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: viewFrame.height - 80))
        self.bottomView.addSubview(self.viewBottomView)
        self.viewBottomView.isHidden = false
        
        self.buttonBottomView = JCNSButtonView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: viewFrame.height - 80))
        self.bottomView.addSubview(self.buttonBottomView)
        self.buttonBottomView.isHidden = true
        
        self.labelBottomView = JCLabelView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: viewFrame.height - 80))
        self.bottomView.addSubview(self.labelBottomView)
        self.labelBottomView.isHidden = true
        
        self.tabBottomView = JCTabView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: viewFrame.height - 80))
        self.bottomView.addSubview(self.tabBottomView)
        self.tabBottomView.isHidden = true
        
        self.tableBottomView = JCNSTableView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: viewFrame.height - 80))
        self.bottomView.addSubview(self.tableBottomView)
        self.tableBottomView.isHidden = true
        
        self.comboBoxView = JCNSComboBoxView(frame: NSRect(x: 0, y: 0, width: viewFrame.width, height: viewFrame.height - 80))
        self.bottomView.addSubview(self.comboBoxView)
        self.comboBoxView.isHidden = true
        
    }
    
    
    // 点击了NSButton的按钮
    @objc func buttonButtonClicked(btn: NSButton) {
        print("============NSButtonClicked==============")
        self.viewBottomView.isHidden = true
        self.buttonBottomView.isHidden = true
        self.labelBottomView.isHidden = true
        self.tabBottomView.isHidden = true
        self.tableBottomView.isHidden = true
        self.comboBoxView.isHidden = true
        
        
        self.viewButton.setupViewBackgroundColor(color: .gray)
        self.btnButton.setupViewBackgroundColor(color: .gray)
        self.labelButton.setupViewBackgroundColor(color: .gray)
        self.tabButton.setupViewBackgroundColor(color: .gray)
        self.tableButton.setupViewBackgroundColor(color: .gray)
        self.comboButton.setupViewBackgroundColor(color: .gray)
        
        
        switch btn.tag {
        case 0:
            self.viewBottomView.isHidden = false
            self.viewButton.setupViewBackgroundColor(color: .blue)
        case 1:
            self.buttonBottomView.isHidden = false
            self.btnButton.setupViewBackgroundColor(color: .blue)
        case 2:
            self.labelBottomView.isHidden = false
            self.labelButton.setupViewBackgroundColor(color: .blue)
            
        case 3:
            self.tabBottomView.isHidden = false
            self.tabButton.setupViewBackgroundColor(color: .blue)
        case 4:
            self.tableBottomView.isHidden = false
            self.tableButton.setupViewBackgroundColor(color: .blue)
        case 5:
            self.comboBoxView.isHidden = false
            self.comboButton.setupViewBackgroundColor(color: .blue)
        default:
            print("")
        }
    }
}

extension JCMainViewController: NSSplitViewDelegate {
    
    func splitView(_ splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) {

//        let currentWindowFrame = NSApplication.shared.keyWindow?.frame ?? NSRect.zero
//        let viewFrame = self.view.frame
//        splitView.frame = currentWindowFrame
        
    }
    
    // 默认设置的是左视图生效，也就是左视图最小宽度为100
    func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
        return 100
    }
    
    // 默认设置的是左视图生效，也就是左视图最大宽度为400
    func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
        return 400
    }
    
    // 默认是false
    func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
        return true
    }
    
    
}
